﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using DiscoveryLogic.Analytics.BusinessLogic.Networks;
using DiscoveryLogic.Analytics.BusinessLogic.Rules;
using DiscoveryLogic.Analytics.DataObjects;
using DiscoveryLogic.Analytics.DataObjects.Dedup;
using DiscoveryLogic.Analytics.DataObjects.Rules;
using DiscoveryLogic.Analytics.DataObjects.Visuals;
using DiscoveryLogic.Common.Utilities;
using UserControl=System.Windows.Controls.UserControl;

namespace DiscoveryLogic.Analytics.Modules.DedupRules
{
    /// <summary>
    /// Interaction logic for NetworkDedupEditor.xaml
    /// </summary>
    public partial class NetworkDedupEditor : UserControl
    {
        public delegate void OnNetworkDedupSettingUpdated(NetworkDedupSetting networkDedupSetting);

        public event OnNetworkDedupSettingUpdated NetworkDedupSettingUpdated;
        private string _NetworkName;

        public NetworkDedupEditor(string networkName)
        {
            InitializeComponent();
            this._NetworkName = networkName;
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.txtNetworkName.Text = this._NetworkName;
            NetworkDedupSetting netDedupSetting = new NetworkDedupSetting();
            netDedupSetting.NetworkName = this._NetworkName;
            netDedupSetting.DedupRules=new Dictionary<EntityType, string>();
            if(NetworkDedupSetting.Exists(this._NetworkName))
            {
                netDedupSetting = NetworkDedupSetting.Read(this._NetworkName);
            }
            this.dvDedupRules.Rows.Clear();
            List<RuleBase> dedupRules = RuleManager.GetRulesByType(RuleType.Dedup, EntityIcons.DedupRuleIcon);
            foreach (EntityType entityType in Enum.GetValues(typeof (EntityType)))
            {
                DataGridViewRow dr = new DataGridViewRow();
                DataGridViewTextBoxCell cellEntityType=new DataGridViewTextBoxCell();
                cellEntityType.Value = entityType.ToString();
                dr.Cells.Add(cellEntityType);
                DataGridViewComboBoxCell cellDedupRules=new DataGridViewComboBoxCell();
                if(dedupRules !=null && dedupRules.Count>0)
                {
                    foreach(RuleBase rule in dedupRules)
                    {
                        cellDedupRules.Items.Add(rule.RuleName);
                    }
                }
                if (netDedupSetting.DedupRules.ContainsKey(entityType))
                {
                    string ruleName = netDedupSetting.DedupRules[entityType];
                    cellDedupRules.Value = ruleName;
                }
                
                dr.Cells.Add(cellDedupRules);
                this.dvDedupRules.Rows.Add(dr);
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {

            NetworkDedupSetting networkDedupSetting = new NetworkDedupSetting();
            networkDedupSetting.NetworkName = this._NetworkName;
            networkDedupSetting.DedupRules = new Dictionary<EntityType, string>();
            foreach (DataGridViewRow dr in this.dvDedupRules.Rows)
            {
                if (!dr.IsNewRow && dr.Cells[0].Value != null && dr.Cells[1].Value != null)
                {
                    EntityType entityType = (EntityType) Enum.Parse(typeof (EntityType), dr.Cells[0].Value.ToString());
                    string ruleName = dr.Cells[1].Value.ToString();
                    networkDedupSetting.DedupRules.Add(entityType, ruleName);
                }
            }
            networkDedupSetting.Save();
            if (this.NetworkDedupSettingUpdated != null)
                this.NetworkDedupSettingUpdated(networkDedupSetting);

            if (this.Parent is Window)
            {
                ((Window) this.Parent).Close();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if(this.Parent is Window)
            {
                ((Window)this.Parent).Close();
            }
        }
    }
}
